package littlewhisker.utils {
	import flash.geom.Point;

	import caurina.transitions.Tweener;
	import flash.events.Event;
	import flash.display.Sprite;
	import littlewhisker.application.mvc.Controller;
	/**
	 * @author Philip
	 * 
	 * Use this static class to hide or show a display object. The mask is removed as soon as the event is over.
	 * The settings property accepts the following aprameters
	 * 
	 */
	 
	public class Masker extends Controller {
		
		public static var UP:String = "up";
		public static var DOWN:String = "down";
		public static var LEFT:String = "left";
		public static var RIGHT:String = "right";
		
		private static var DEFAULT_TRANSITION_TIME:Number = 0.4;
		private static var DEFAULT_TRANSITION_TYPE:String = "easeOutQuint";
		private static var DEFAULT_DIRECTION:String = "LEFT";
		
		
	
		/**
		 * @param revealed : Boolean
		 * if true, the target will start off visible and inteds to hide during the tween
		 * if true, the target will start off invisible and inteds to reveal during the tween
		 * 
		 * @param view : Sprite
		 * the displayobject where your target is positioned
		 * 
		 * @param target : Sprite
		 * the display object you want to mask
		 * 
		 * @param settings : Object (optional)
		 * direction: String - use the constants whitin this class UP, DOWN, LEFT, RIGHT.
		 * time: transition time in seconds
		 * startX: x location of where the mask starts
		 * startY: y location of where the mask starts
	 	 * destinationX: x location where the mask will tween to
		 * destinationY: y location where the mask will tween to
		 */
		 
			
		public static function execute(revealed:Boolean, view:Sprite, target:Sprite, settings:Object ):void{
			var startX:Number = 0;
			var startY:Number = 0;
			
			var destinationX:Number = 0;
			var destinationY:Number = 0;
			
			var direction:String;
			settings.direction ? direction = settings.direction : direction = DEFAULT_DIRECTION;
			
			var point:Point = getPosition(target, direction);
			
			if(revealed){
				destinationX = point.x;
				destinationY = point.y;
			} else {
				startX = point.x;
				startY = point.y;
			}
			
			if(settings.startX)  startX = settings.startX;
			if(settings.startY)  startY = settings.startY;
					
			if(settings.destinationX) destinationX = settings.destinationX;
			if(settings.destinationY) destinationY = settings.destinationY;
			
			var time:Number;
			settings.time ? time = settings.time : time = DEFAULT_TRANSITION_TIME;
			
			var type:String;
			settings.type ? type = settings.type : type = DEFAULT_TRANSITION_TYPE;
			
			var holder:Sprite = new Sprite();
			holder.x = target.x;
			holder.y = target.y;
			
			view.addChild(holder);
			
			var masker:Sprite = new Sprite();
			holder.addChild(masker);
			masker.graphics.beginFill(0x00aa00);
			masker.graphics.drawRect(0, 0, target.width, target.height);
			target.mask = masker;
			
			masker.x = startX;
			masker.y = startY;
							
			var tempOnUpdate:Function = function():void{
				holder.x = target.x;
				holder.y = target.y;
			}
			
			var tempOnComplete:Function = function():void{
				target.mask = null;
				view.removeChild(holder);
				//if(settings.event) dispatchEvent(settings.event as Event);
				if(settings.callback) {
					var arguments:Array;
					settings.arguments ? arguments = settings.arguments : arguments = new Array();
					settings.callback.apply(arguments);
				}
			}
					
			Tweener.addTween(masker,{time:time,x:destinationX, y:destinationY, transition:type, onComplete:tempOnComplete, onUpdate:tempOnUpdate});
		}
		
		private static function getPosition(target:Sprite, direction:String):Point{
			var point:Point = new Point();
			switch (direction){
				case UP:
					point.x = target.x;
					point.y = -target.height;
					break;
				case DOWN:
					point.x = target.x;
					point.y = -target.height;
					break;
				case LEFT:
					point.x = -target.width;
					point.y = target.y;
					break;
				case RIGHT:
					point.x = target.width;
					point.y = target.y;
					break;
			}
			return point;
		}
		
	}	
	
}